/* 登录页
 ================================== */
$(function () {
    var loginDOM = $(".js_loginbutton"),
        errorDOM = $('.note');
    // 验证的表单name值
    var valid = {
        loginName: {
            required: true,
            message: "账号不能为空"
        },
        password: {
            required: true,
            message: "密码不能为空"
        },
        validateCode: {
            required: true,
            message: '请输入验证码',
            validType: validCode,
            validMessage: '验证码不正确'
        }
    };

    var login = {
        encryptionPassword: function(){
            var hash = $.md5($.md5($("input[type='password']").val().toUpperCase()).toUpperCase()).toLowerCase();
            $("input[name='password']").val(hash);
        },
        // 验证表单
        checkForm: function() {
            var result = false;
            $.each(valid, function (i, n) {
                var input;
                if(i == "password"){
                    input = $('input[type=' + i + ']')
                }else{
                    input = $('input[name=' + i + ']')
                }
                if (n.required && !$.trim(input.val())) {
                    login.addTip(input, n.message);
                    result = false;
                    return result;
                } else {
                    result = true;
                }
            });
            return result;
        },
        // 加入提示消息
        addTip: function(input, tip) {
            errorDOM.css({ "display": "block" }).html(tip);
        },
        submitForm: function(){
            var _form=document.forms[0];
            if (login.checkForm()){
                login.encryptionPassword();
                loginDOM.val("登录中...").attr("disabled", "disabled").css({"background-color":"rgba(105, 163, 219, .6)","text-decoration":"none"});
                _form.submit();
            }else{
                _form.onSubmit=false;
                return false;
            }
        },
        sendForm: function(){
            var FORM = new FormData();

        },
        init: function(){
            if (message!='') {
                login.addTip(loginDOM, message);
            }

            // 获取焦点事件
            $('input').on('focus', function () {
                errorDOM.css("display","none");
            });

            // 绑定回车键事件
            $(document).keyup(function(event){
                if(event.keyCode ==13){
                    loginDOM.trigger('click');
                }
            });

            loginDOM.on('click', function (e) {
                e.preventDefault();
                login.submitForm()
            });

            // 绑定回车键事件
            $(document).keyup(function(event){
                if(event.keyCode ==13){
                    loginDOM.trigger('click');
                }
            });
        }
    }
   // 更换验证码链接事件
    $('#changeCode').bind('click', function () {
        $('.validImg').attr('src',  '/validateCode?' + Math.random());
        return false;
    });
    // 重新获取验证码
    $(".validImg").click(function () {
        $(this).attr("src",  "/validateCode?" + Math.random());
    });
    login.init();
});